<?php

class PayoutReportController extends Controller
{
	public function actionIndex()
	{
		$connection = Yii::app()->db;

        if ( $_POST ) {
        	$year = $_POST['year'];
        }
		else {
			$year = date("Y");
		}
		
		$sql = "SELECT year, month_no, SUM(payout) AS total_payout, SUM(bv_personal) as total_bv
		               FROM gn_payouts 
		              WHERE year=$year AND member_id > 1
		               GROUP BY year, month_no
		               ORDER BY month_no ASC ";
		
		$command = $connection->createCommand($sql);
	    $dataReader = $command->query();

        $payout_data = array();
	    while ( $data = $dataReader->read() ) {
	    	array_push($payout_data, $data);
		}  	
		
		$this->render('index', array( 'payout_data'=> $payout_data ) );
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	
	public function actionExcelPayouts()
	{
		$month_no = $_GET['w'];
		$year = $_GET['y'];
		
		$connection = Yii::app()->db;
		
		$sql = "SELECT a.first_name, a.last_name, b.* 
		          FROM gn_members AS a 
		     LEFT JOIN gn_payouts AS b
		            ON (a.member_id=b.member_id)
		         WHERE b.month_no=$month_no
		           AND b.year=$year
		      ORDER BY b.member_id";
					  
		$command = $connection->createCommand($sql);
	    $result = $command->query()->readAll();   
	    		
		$filename = "payouts-$year-$month_no.csv";
		header('HTTP/1.1 200 OK');
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"$filename\"");
		
		$header = "payout_id, member_id, first_name, last_name, year, month_no, bv_personal, bv_left, bv_right, bv_left_carryover, bv_right_carryover, bv_match, payout, status, dt_created \n";
		echo $header;
		
		$payouts = array();   
		if ( count($result) > 0 ) {
			foreach ( $result as $node ) {
			  
			  $data=array();	
			  $data['payout_id'] = $node['payout_id'];
			  $data['member_id'] = $node['member_id'] ;
			  $data['first_name'] = $node['first_name'];
			  $data['last_name'] = $node['last_name'] ;
			  $data['year'] = $node['year'];
			  $data['month_no'] = $node['month_no'];
			  $data['bv_personal'] = $node['bv_personal'];
			  $data['bv_left'] = $node['bv_left'];
			  $data['bv_right'] = $node['bv_right'];
			  $data['bv_left_carryover'] = $node['bv_left_carryover'];
			  $data['bv_right_carryover'] = $node['bv_right_carryover'];
			  $data['bv_match'] = $node['bv_match'];
			  $data['payout'] = $node['payout'];
			  $data['status'] = $node['status'];
			  $data['dt_created'] = $node['dt_created'];
			  
			  //array_push( $payouts, $data );
			  $strdata = implode( ",", $data);
			  echo "$strdata\n";
			}
		}
	
		//$dataProvider = new CArrayDataProvider($payouts);
		//$pagination = new CPagination(count($payouts));
		//$pagination->setPageSize(1024);
        //$dataProvider->setPagination($pagination);
	    //$this->toExcel($dataProvider); 


	}
  
    public function behaviors(){
	
	    return array(
            'eexcelview'=>array(
                'class'=>'ext.eexcelview.EExcelBehavior',
            ),
        );
	}
}